package com.cogent.business.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.common.util.MapUtil;
import com.cogent.dao.BussinessInstance;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dao.type.DistrictControl;
import com.cogent.report.domain.ReportSearchFieldDomain;

public class ReportHelper {	
	public static String genQueryField(ReportSearchFieldDomain formColumnDomain, HashMap formMap) {
		String[] typecodes = formColumnDomain.getTypecode().split("\\|");
		String sql = typecodes[0];
		String orderby = typecodes[1];
		List queryList = new ArrayList();
		HashMap curQueryMap = new HashMap();
		if(typecodes.length > 2) {
			String[] param = typecodes[2].split(";");
			for(int i=0; i<param.length; i++) {
				String[] curtemps = param[i].split("~");
				String value = MapUtil.getStringValue(formMap, curtemps[0]);
				if(curtemps[0].equals("districtcode"))
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], DistrictControl.getControlCode(value), DistrictControl.getQueryType(value)));
				else
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], value));
				curQueryMap.put(curtemps[0], value);
			}
		}
		String defaultValue = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		int keyindex = 1;
	 
		List list = BussinessQueryHelper.query(sql, queryList, orderby);
		 
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName()).append("\"/>");
		int level = 1;
		str.append(getOption("--请选择","", defaultValue, level));
		if(list.size() > 0) {
			HashMap curMap = (HashMap)list.get(0);
			level = MapUtil.getIntValue(curMap, "organlevel", 1) - 1;
		}
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			String[] options = MapUtil.getStringValue(dataMap, "enumvalue").split("~");
			str.append(getOption(MapUtil.getStringValue(dataMap, "enumoption"), options[keyindex], defaultValue,
						MapUtil.getIntValue(dataMap, "enumlevel") - level));
		}
		str.append("</select>");
		
		return str.toString();
	}
	public static String getOption(String option, String value, String curCode, int level) {
		String st = "";
		for(int i=1; i<level; i++)
			st += "--";
		StringBuffer str = new StringBuffer();
		str.append("<option value=\"").append(value).append("\"");
		if(value.equals(curCode))
			str.append(" selected=\"selected\"");
		str.append(">").append(st).append(option).append("</option>");
		
		return str.toString();
	}
	public static int getRank(List list,Integer num){		
		for(int i=1;i<list.size()+1;i++){
			Double temp=Double.valueOf(String.valueOf(list.get(list.size()-i)));
			if (num >= temp){
				return i;
				
			}
		}
		return list.size();
	}
}
